local jiuyirushuang = fk.CreateSkill{
    name = "rmt__jiuyirushuang",
    tags = {Skill.Compulsory},
}
Fk:loadTranslationTable{
    ["rmt__jiuyirushuang"] = "旧忆如霜",
    [":rmt__jiuyirushuang"] = "锁定技，你的手牌数和体力值均发生过变化的回合结束时，你视为使用你已失去且未再次得到过的X张花色各不同的即时牌（X为中央区花色数），均结算后，其中每有一张牌：未被抵消，你便获得之对应的实体牌；没有生效，你便依次对自己造成1点冰冻伤害。",
    ["@$rmt__jiuyirushuang"] = "旧忆如霜",
    ["#rmt__jiuyirushuang-use"] = "旧忆如霜：请使用一张牌",
}

jiuyirushuang:addEffect(fk.TurnEnd, {
    can_trigger = function (self, event, target, player, data)
        if player:hasSkill(jiuyirushuang.name) then
        return #player.room.logic:getEventsOfScope(GameEvent.MoveCards, 1, function(e)
            for _, move in ipairs(e.data) do
                if move.to == player and move.toArea == Card.PlayerHand then
                    return true
                end
                if move.from == player then
                    for _, info in ipairs(move.moveInfo) do
                        if info.fromArea == Card.PlayerHand then
                            return true
                        end
                    end
                end
            end
        end, Player.HistoryTurn) > 0 and #player.room.logic:getEventsOfScope(GameEvent.ChangeHp, 1, function (e)
            return e.data.who == player
        end, Player.HistoryTurn) > 0
        end
    end,
    on_use = function (self, event, target, player, data)
        local room = player.room
        local suits, used = {}, {}
        for _, id in ipairs(Fk:currentRoom():getBanner("@$CenterArea")) do
            table.insertIfNeed(suits, Fk:getCardById(id).suit)
        end
        table.removeOne(suits, Card.NoSuit)
        local gainCards = {}
        while #used < #suits do
            local lose, gain = player:getTableMark("rmt__jiuyirushuang_lose"), player:getTableMark("rmt__jiuyirushuang_gain")
            lose = table.filter(lose,function (id)
                local card = Fk:getCardById(id)
                return (card.type == Card.TypeBasic or card:isCommonTrick()) and not table.contains(used,card.suit)
            end)
            gain = table.filter(gain,function (id)
                local card = Fk:getCardById(id)
                return (card.type == Card.TypeBasic or card:isCommonTrick()) and not table.contains(used,card.suit)
            end)
            local marks = {}
            for _, id in ipairs(lose) do
                if not table.contains(gain,id) and not table.contains(used,Fk:getCardById(id).suit) then
                    table.insert(marks,id)
                end
            end
            if #marks > 0 then
                local use = U.askForUseRealCard(room,player,marks,nil,jiuyirushuang.name,"#rmt__jiuyirushuang-use",{
                    bypass_times = true,
                    extraUse = true,
                    expand_pile = marks,
                },false,false)
                if use then
                    table.insert(gainCards,use.card.id)
                    table.insert(used,use.card.suit)
                else
                    break
                end
            else
                break
            end
        end

        local cancelled = {}
        player.room.logic:getEventsOfScope(GameEvent.UseCard, 1, function (e)
            local use = e.data
            if use and (use.cardsResponded and #use.cardsResponded > 0) and table.contains(gainCards,use.card.id) then
            table.insertIfNeed(cancelled, use.card.id)
            end
        end, Player.HistoryTurn)
        gainCards = table.filter(gainCards,function (id)
            return not table.contains(cancelled,id)
        end)
        if #gainCards > 0 then
            room:obtainCard(player,gainCards,true)
        end
        if #cancelled > 0 then
            for i = 1, #cancelled, 1 do
                room:damage{
                    from = player,
                    to = player,
                    damage = 1,
                    damageType = fk.IceDamage,
                    skillName = jiuyirushuang.name,
                }
            end
        end
    end,
})

jiuyirushuang:addEffect(fk.AfterCardsMove, {
    can_refresh = function (self, event, target, player, data)
        local mark = player:getTableMark("rmt__jiuyirushuang_lose")
        for _, move in ipairs(data) do
            if move.from == player and
            not (move.to == player and (move.toArea == Card.PlayerHand or move.toArea == Card.PlayerEquip)) then
                for _, info in ipairs(move.moveInfo) do
                    if info.fromArea == Card.PlayerHand or info.fromArea == Card.PlayerEquip then
                        return true
                    end
                end
            end
            if move.to == player and move.toArea == Card.PlayerHand then
                for _, info in ipairs(move.moveInfo) do
                    if table.contains(mark,info.cardId) then
                        return true
                    end
                end
            end
        end
    end,
    on_refresh = function (self, event, target, player, data)
        local room = player.room
        local lose, gain = player:getTableMark("rmt__jiuyirushuang_lose"), player:getTableMark("rmt__jiuyirushuang_gain")
        for _, move in ipairs(data) do
            if move.from == player and
            not (move.to == player and (move.toArea == Card.PlayerHand or move.toArea == Card.PlayerEquip)) then
                for _, info in ipairs(move.moveInfo) do
                    if info.fromArea == Card.PlayerHand or info.fromArea == Card.PlayerEquip then
                        table.insertIfNeed(lose, info.cardId)
                    end
                end
            end
            if move.to == player and move.toArea == Card.PlayerHand then
                for _, info in ipairs(move.moveInfo) do
                    if table.contains(lose,info.cardId) then
                        table.insertIfNeed(gain, info.cardId)
                    end
                end
            end
        end
        lose = table.filter(lose,function (id)
            local card = Fk:getCardById(id)
            return card.type == Card.TypeBasic or card:isCommonTrick()
        end)
        gain = table.filter(gain,function (id)
            local card = Fk:getCardById(id)
            return card.type == Card.TypeBasic or card:isCommonTrick()
        end)
        room:setPlayerMark(player,"rmt__jiuyirushuang_lose",lose)
        room:setPlayerMark(player,"rmt__jiuyirushuang_gain",gain)

        local marks = {}
        for _, id in ipairs(lose) do
            if not table.contains(gain,id) then
                table.insert(marks,id)
            end
        end
        if player:hasSkill(jiuyirushuang.name) and #marks > 0 then
            room:setPlayerMark(player,"@$rmt__jiuyirushuang",marks)
        else
            room:setPlayerMark(player,"@$rmt__jiuyirushuang",0)
        end
    end,
})

jiuyirushuang:addAcquireEffect(function (self, player, is_start)
    local room = player.room
    local lose, gain = player:getTableMark("rmt__jiuyirushuang_lose"), player:getTableMark("rmt__jiuyirushuang_gain")
    local marks = {}
    room:addSkill("#CenterArea")
    for _, id in ipairs(lose) do
        if not table.contains(gain,id) then
            table.insert(marks,id)
        end
    end
    if player:hasSkill(jiuyirushuang.name) and #marks > 0 then
        room:setPlayerMark(player,"@$rmt__jiuyirushuang",marks)
    else
        room:setPlayerMark(player,"@$rmt__jiuyirushuang",0)
    end
end)

jiuyirushuang:addLoseEffect(function (self, player, is_death)
    player.room:setPlayerMark(player,"@$rmt__jiuyirushuang",0)
end)

return jiuyirushuang